using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;

namespace BySoft.Server.DataAccess
{
	public partial class MdExchangerateDA
	{
		#region GetByID

		/// <summary>
		/// 取得实体
		/// </summary>
		/// <param name="id">实体关键字</param>
		/// <returns></returns>
		public static MdExchangerateEntity GetMdExchangerateByID(Int32? id)
		{
			string strSQL = "SELECT * FROM " + MdExchangerateTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddAndCondition(MdExchangerateTable.C_EXCHANGERATEID, SQLOperator.Equal, id);
			return OneSoft.DataAccess.DataAccessCommon.SelectSingle<MdExchangerateEntity>(wb);
		}

		#endregion

		#region GetAll

		/// <summary>
		/// 获取所有数据
		/// </summary>
		/// <returns>所有数据集合</returns>
		public static DataSet GetMdExchangerateAll()
		{
			string strSQL = "SELECT * FROM " + MdExchangerateTable.C_TABLENAME;
			return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(strSQL);
		}

		#endregion

		#region GetByCondition

		/// <summary>
		/// 根据Condition获取DS
		/// </summary>
		/// <returns>所有数据集合</returns>
		public static DataSet GetDSByCondition(string strCondition)
		{
			string strSQL = "SELECT * FROM " + MdExchangerateTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddCondition(strCondition);
			return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(wb);
		}

		/// <summary>
		/// 根据Condition获取实体集合
		/// </summary>
		/// <param name="strCondition">SQL条件</param>
		/// <returns>实体集合</returns>
		public static List<MdExchangerateEntity> GetByCondition(string strCondition)
		{
			string strSQL = "SELECT * FROM " + MdExchangerateTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddCondition(strCondition);
			return OneSoft.DataAccess.DataAccessCommon.Select<MdExchangerateEntity>(wb);
		}

		#endregion

        public static DataTable SelectList(string Currency, int pageIndex, int pageSize, out int m_count)
        {
            try
            {
                #region SQL Server
                string strcmd = @"select e.*,p.ParameterName as CurrencyName from MD_ExchangeRate e
                                join SYS_Parameter p on p.ParameterCode=e.Currency and p.ParameterTypeID='Currency'
                                where e.Status='1'";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(Currency))
                {
                    strcmd += " and e.Currency = @Currency";
                    dpc.AddWithValue("@Currency", Currency);
                }

                strcmd += " ORDER BY e.ExchangeRateID DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, pageSize, dpc, out m_count);
                #endregion
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 根据币别选取当前汇率
        /// </summary>
        /// <param name="currency">币别</param>
        /// <returns></returns>
        public static MdExchangerateEntity GetMaxMdExchangerateByCurrency(string currency)
        {
            try
            {
                #region SQL Server
                string strcmd = @"select top 1 e.* from MD_ExchangeRate e
                                where e.Status='1'";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(currency))
                {
                    strcmd += " and e.Currency = @Currency";
                    dpc.AddWithValue("@Currency", currency);
                }

                strcmd += " ORDER BY e.ExchangeRate DESC";

                return DataAccessCommon.SelectSingle<MdExchangerateEntity>(strcmd, dpc);
                #endregion
            }
            catch
            {
                throw;
            }
        }
    }
}

